const express = require("express");
const svgCaptcha = require('svg-captcha');

const config = require("../../config/config");
const { md5 } = require("../../model/tools");
const ManagerModel = require("../../model/managerModel");

const router = express.Router();

router.get("/", async (req, res) => {
    /* let result = new ManagerModel({
        username: "admin",
        password: md5("123456")
    })
    await result.save(); */
    res.render("admin/login/login.html");
})

// 生成图片验证码
router.get("/verify", function (req, res) {
    const captcha = svgCaptcha.create();
    // 保存验证码
    req.session.captcha = captcha.text;
    res.type('svg');
    res.status(200).send(captcha.data);
});

router.post("/doLogin", async (req, res) => {
    // 获取登录信息
    let username = req.body.username;
    let password = req.body.password;
    let verify = req.body.verify;
    // 1. 判断验证码是否正确
    if(verify.toLocaleLowerCase() != req.session.captcha.toLocaleLowerCase()) {
        res.render("admin/public/error.html", {
            "redirectUrl": `/${config.adminPath}/login`,
            "message": "图形验证码输入错误"
        });
        return;
    }

    // 2. 判断用户名密码是否合法
    let result = await ManagerModel.find({username, password: md5(password)});
    if(result.length > 0) {
        // 保存用户信息
        req.session.userinfo = result[0];

        // 验证成功
        res.render("admin/public/success.html", {
            "redirectUrl": `/${config.adminPath}`,
            "message": "登录成功"
        });
    } else {
        res.render("admin/public/error.html", {
            "redirectUrl": `/${config.adminPath}/login`,
            "message": "用户名或者密码错误"
        });
    }
});

router.get("/loginOut", (req, res) => {
    req.session.userinfo = null;
    res.redirect(`/${config.adminPath}/login`);
})

module.exports = router